<template>
    <!-- 添加 -->
    <div class="add-box">
        <div class="data-list">
            <el-icon>
                <Tickets />
            </el-icon>
            <span>数据列表</span>
        </div>
        <el-button type="default"
            @click="router.push(`/pms/addProductAttr?cid=${route.query.cid}&type=${route.query.type}`)">
            添加</el-button>
    </div>
    <el-table :data="proAttributeList" border fit @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="name" label="属性名称"></el-table-column>
        <el-table-column label="商品类型">
            <template #default="scope">
                {{ route.query.cname }}
            </template>
        </el-table-column>
        <el-table-column prop="selectType" label="属性是否可选">
            <template #default="scope">
                {{ scope.row.selectType === 0 ? '唯一' : (scope.row.selectType === 1 ? '单选' : '多选') }}
            </template>
        </el-table-column>
        <el-table-column prop="inputType" label="属性值的录入方式">
            <template #default="scope">
                {{ scope.row.inputType === 0 ? '手工录入' : '从列表中选取' }}
            </template>
        </el-table-column>
        <el-table-column prop="inputList" label="可选值列表">
        </el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column label="操作" width="160">
            <template #default="scope">
                <el-button type="default" size="small" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="bottom">
        <!-- 批量操作 -->
        <div>
            <el-select v-model="deleteVal" class="m-2" placeholder="批量操作" size="large">
                <el-option label="删除" value="del" />
            </el-select>
            <el-button type="primary" @click="sureOperate">确定</el-button>
        </div>
        <!-- 分页 -->
        <ElConfigProvider :locale="zhCn">
            <el-pagination v-model:currentPage="pageNum" v-model:page-size="pageSize" background
                layout="total, sizes, prev, pager, next ,jumper" :total="total" @size-change="pageSizeChange"
                @current-change="pageNumChange" :page-sizes="[5, 10, 15]" />
        </ElConfigProvider>
    </div>
</template>
<script setup lang='ts'>
import { Tickets } from '@element-plus/icons-vue';
import { pro } from '@/api'
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider, ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref, toRefs } from 'vue';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter()
const route = useRoute()
let deleteVal = ref<string>('')
let delIDS = ref<number[]>([])
interface Istate {
    proAttributeList: IProductAttributeModel[],
    pageNum: number,
    pageSize: number,
    total: number
}
const state = reactive<Istate>({
    proAttributeList: [],
    pageNum: 1,
    pageSize: 5,
    total: 0
})
const { proAttributeList, pageNum, pageSize, total } = toRefs(state)

// 封装分页请求
const getAttrList = () => {
    pro.getProductAttributeListApi({ cid: parseInt(route.query.cid as string), pageNum: state.pageNum, pageSize: state.pageSize, type: parseInt(route.query.type as string) }).then(res => {
        state.proAttributeList = res.data.list;
        state.total = res.data.total
    })
}
getAttrList()
const pageSizeChange = () => {
    getAttrList()
}
const pageNumChange = () => {
    getAttrList()
}
// 批量操作:删除
const sureOperate = () => {
    if (deleteVal.value === 'del') {
        delProAttr(delIDS.value)
    }
}
// 表格全选、单选
const handleSelectionChange = (data: IProductAttributeModel[]) => {
    delIDS.value = data.map(item => item.id)
}
// 编辑
const editFn = (id: number) => {
    router.push(`/pms/updateProductAttr?id=${id}`)
}
// 删除
const delFn = (id: number) => {
    delProAttr([id])
}
// 封装删除
const delProAttr = (ids: number[]) => {
    ElMessageBox.confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        pro.deleteProductAttributeApi({ ids }).then(res => {
            if (res.code === 200) {
                ElMessage.success('删除成功')
                getAttrList()
            }
        })
    })
}
</script>
<style lang="less" scoped>
.add-box {
    padding: 15px;
    display: flex;
    justify-content: space-between;
    border: 1px solid #ebeef5;
    border-radius: 5px;
    align-items: center;
    margin-bottom: 20px;

    .data-list {
        display: flex;
        align-items: center;
    }
}

.bottom {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;

    .el-button {
        margin-left: 20px;
    }
}
</style>